/**
 * 
 */
package com.sendMessage.demo.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.sendFile.demo.socket.socketfile.ServerTest;

/**
 * @author 周成瑜
 *
 */
public class ServiceClient {
	
	int port=12345;
	
	void start(){
		Socket socket = null;
		
		try {
			
			ServerSocket serverSocket = new ServerSocket(port);
			
			while(true){//一直循环监听
			
			System.out.println("等待客户端连接，连接端口：" + port);
			socket = serverSocket.accept();
			
			// 读取客户端数据 
            InputStream info = socket.getInputStream(); 
            DataInputStream dis = new DataInputStream(info); 
            String s=dis.readUTF();
            System.out.println(s); 
            
            // 向客户端输出数据 
            OutputStream os = socket.getOutputStream(); 
            DataOutputStream dos = new DataOutputStream(os); 
            dos.writeUTF("我是服务端，我收到了客户端的消息!"+s); 
            dos.flush(); 

            //关闭流
            dos.close();
            os.close();
            dis.close();
            info.close();
            
			}
		}catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public static void main(String[] args) {
		new ServiceClient().start();
	}
}
